use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "article")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub title: String,
    pub content: String,
    pub source_url: String,
    pub spider_task_id: i32,
    pub breadcrumb: String,
    pub created_at: i64,
    pub updated_at: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::spider_task::Entity",
        from = "Column::SpiderTaskId",
        to = "super::spider_task::Column::Id"
    )]
    SpiderTask,
}

impl Related<super::spider_task::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::SpiderTask.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

// Optional: Data Transfer Object for the Article
#[derive(Debug, Serialize, Deserialize)]
pub struct ArticleDTO {
    pub id: i32,
    pub title: String,
    pub content: String,
    pub source_url: String,
    pub spider_task_id: i32,
    pub breadcrumb: String,
    pub created_at: Option<i64>,
    pub updated_at: Option<i64>,
}

impl From<Model> for ArticleDTO {
    fn from(model: Model) -> Self {
        Self {
            id: model.id,
            title: model.title,
            content: model.content,
            source_url: model.source_url,
            spider_task_id: model.spider_task_id,
            breadcrumb: model.breadcrumb,
            created_at: Some(model.created_at),
            updated_at: Some(model.updated_at),
        }
    }
}